package com.bjmashibing.system.huawei.buf_recv;


import java.util.Arrays;
import java.net.DatagramPacket;
import java.net.InetAddress;


import com.bjmashibing.system.huawei.Cmd_fun;
import com.bjmashibing.system.huawei.SocketNIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class Cbuffer_machine {
	private final static Logger logger = LoggerFactory.getLogger(SocketNIO.class);
	public static synchronized  boolean buffer_get(byte cmd[]) {
//		if(buf_machine.bflag_synchronized)
		{
			try{
				byte[] p;
				boolean ret_cmp = false;
				boolean ret_cmp2 = false;
				int i;
				byte[] array_ack = new byte[5];
				byte[] array1 = new byte[9];
				byte[] array2 = new byte[420];
				byte[] cmd2 = new byte[1024];
				byte[] cmd3 = new byte[1024];
				byte[] cmd4 = new byte[1024];
				byte[] buf_len = new byte[5];
				/*
				 * Arrays.fill(array1, 0); Arrays.fill(array2, 0);
				 */
				int availability_len = 0;
				if (buf_machine.nHead_machine <= buf_machine.nTail_machine)
				{
					availability_len = buf_machine.nTail_machine- buf_machine.nHead_machine;
				}
				else {
					availability_len = buf_machine.BUF_SIZE - buf_machine.nHead_machine
							+ buf_machine.nTail_machine;
				}
				if (availability_len >= 97) //wifi
				{

						int[] len = new int[1];
						Boolean flag_temp = handel_cmd_net(cmd,len);
						//Log.i("buffer_get,len[0]=" + len[0]);
						if(flag_temp){
							return true;
						}else{
							return false;
						}

					}
					

			}
			catch(Exception e){
				buf_machine.bflag_synchronized = false;
				logger.info("buffer_get,,,,,,,,,,,,,exception error");
				return false;
			}
			buf_machine.bflag_synchronized = false;
			return false;
		}
//		buf_machine.bflag_synchronized = false;
//		return false;
	}

	public static void buffer_init() {
		try{
			buf_machine.bflag_synchronized = false;
			buf_machine.nums_circle = 0;
			buf_machine.nTail_machine = 0;
			buf_machine.nHead_machine = 0;
			byte[] buf_m_temp = new byte[buf_machine.BUF_SIZE];
			System.arraycopy(buf_m_temp, 0, buf_machine.buf_m,0, buf_machine.BUF_SIZE);
			/*for(int i =0;i < 100;i++){
				Log.i("buf_machine.buf_m[" + i + "] = " + buf_machine.buf_m[i]);
			}*/
		}
		catch(Exception e){
			logger.info("buffer_init,,,,,,,,,,,,,exception error");
		}
		
	}
	public static synchronized  void buffer_save(int len_recv, byte[] cmd) {
//		if(!buf_machine.bflag_synchronized)
		{
			try{
				
				int availability_len_get = 0;
				if (buf_machine.nHead_machine <= buf_machine.nTail_machine) {
					availability_len_get = buf_machine.nTail_machine
							- buf_machine.nHead_machine;
				} else {
					availability_len_get = buf_machine.BUF_SIZE
							- buf_machine.nHead_machine + buf_machine.nTail_machine;
				}

				// ///////////////////////////////////////////////////////////
				int availability_len = 0;
				if (buf_machine.nHead_machine <= buf_machine.nTail_machine) {
					availability_len = buf_machine.BUF_SIZE - buf_machine.nTail_machine
							+ buf_machine.nHead_machine;
				} else {
					availability_len = buf_machine.nHead_machine
							- buf_machine.nTail_machine;
				}
				if (availability_len > len_recv) {
					int p1 = buf_machine.nTail_machine + len_recv;
					if (p1 > buf_machine.BUF_SIZE) {
						int len_back = p1 - buf_machine.BUF_SIZE;
						int len_front = len_recv - len_back;
						System.arraycopy(cmd, 0, buf_machine.buf_m,
								buf_machine.nTail_machine, len_front);
						System.arraycopy(cmd, len_front, buf_machine.buf_m, 0, len_back);
						buf_machine.nTail_machine = len_back % buf_machine.BUF_SIZE;
					} else {
						System.arraycopy(cmd, 0, buf_machine.buf_m,
								buf_machine.nTail_machine, len_recv);
						buf_machine.nTail_machine = p1 % buf_machine.BUF_SIZE;
					}
				}
				buf_machine.bflag_synchronized = true;
			}
			catch(Exception e){
				buf_machine.bflag_synchronized = true;
				logger.info("buffer_save,,,,,,,,,,,,,exception error");
			}
			
		}
	}

	public static Boolean handel_cmd_net(byte cmd[],int len[]) {
		try{
			byte[] p;
			boolean ret_cmp = false;
			boolean ret_cmp2 = false;
			int i;
			byte[] array_ack = new byte[5];
			byte[] array_null = new byte[6];
			byte[] array1 = new byte[9];
			byte[] array2 = new byte[420];
			byte[] cmd2 = new byte[1024];
			byte[] cmd3 = new byte[1024];
			byte[] cmd4 = new byte[1024];
			byte[] buf_len = new byte[5];
			int availability_len = 0;
			if (buf_machine.nHead_machine <= buf_machine.nTail_machine)
			{
				availability_len = buf_machine.nTail_machine- buf_machine.nHead_machine;
			}
			else {
				availability_len = buf_machine.BUF_SIZE - buf_machine.nHead_machine
						+ buf_machine.nTail_machine;
			}
			
			for (i = 0; i < buf_machine.BUF_SIZE; i++) 
			{
				int index_1 = (buf_machine.nHead_machine + i)
						% buf_machine.BUF_SIZE;
				if (buf_machine.buf_m[index_1] == '"') {

					for (i = 0; i < 9; i++) {
						int index22 = (buf_machine.nHead_machine + 9 + i)
								% buf_machine.BUF_SIZE;
						array1[i] = buf_machine.buf_m[index22];
					}
					for (i = 0; i < 9; i++) {
						int index22 = (buf_machine.nHead_machine + i)
								% buf_machine.BUF_SIZE;
						array2[i] = buf_machine.buf_m[index22];
					}

					String acct_id = "\"HENG-BO\"";
					String acct_id2 = "\"ACK\"";
					String acct_id3 = "\"NULL\"";
					String res = new String(array1);
//					Log.i(	"handel_cmd_net，array1=" + res);
					

					ret_cmp = Arrays.equals(array1, acct_id.getBytes());
					
					if(!ret_cmp)
					{
						System.arraycopy(array1, 0, array_ack, 0, 5);
						ret_cmp = Arrays.equals(array_ack, acct_id2.getBytes());
					}
					if(!ret_cmp)
					{
						System.arraycopy(array1, 0, array_null, 0, 6);
						ret_cmp = Arrays.equals(array_null, acct_id3.getBytes());
					}
					if (ret_cmp) 
					{
						//Log.i("wyh",	"fffffffffffffffffffffffffffffffffffffffffffff");
						/*int len_std_a = 0;
						int len_std_b = 0;
						if (array2[7] < 58) {
							len_std_a = array2[7] - 48;
						} else if ((array2[7] >= 65) && (array2[7] <= 70)) {
							len_std_a = array2[7] - 55;
						}
						if (array2[8] < 58) {
							len_std_b = array2[8] - 48;
						} else if ((array2[8] >= 65) && (array2[7] <= 70)) {
							len_std_b = array2[8] - 55;
						}*/
//						int len_standard = len_std_a * 16 + len_std_b;


						int len_standard = Cmd_fun.get_len_standard(array2);
						if (len_standard <= 0) {
							logger.info("error,cmd_analysis_1,len_standard=" + len_standard);
							return false;
						}


						int len_2 = len_standard + 10 + 2;
						//Log.i("wyh", "len_standard=" + len_standard + " len_2="
								//+ len_2);

						for (i = 0; i < len_2; i++) {
							int index11 = (buf_machine.nHead_machine + i)
									% buf_machine.BUF_SIZE;
							array2[i] = buf_machine.buf_m[index11];

						}

//						int len_pri = array2[len_standard + 10] * 256
//								+ array2[len_standard + 10 + 1];
						int len_pri = array2[len_standard + 10] * 256 + (int) (array2[len_standard + 10 + 1] & 0xff);
						int len_total = len_standard + len_pri + 10 + 2 + 4;
						//Log.i("buffer_get,len_total=" + len_total + " ,availability_len=" + availability_len);
						//Log.i("wyh", "len_pri=" + len_pri + " len_total="
								//+ len_total);
						if(len_total > availability_len){
							logger.info("buffer_get,no enough data,waiting next data,len_total=" + len_total + " ,availability_len=" + availability_len);
							buf_machine.bflag_synchronized = false;
							return false;
						}
						for (i = 0; i < len_total; i++) {
							int index11 = (buf_machine.nHead_machine + i)
									% buf_machine.BUF_SIZE;
							cmd[i] = buf_machine.buf_m[index11];
						}
						for (i = 0; i < len_total; i++) {
							buf_machine.nHead_machine = (buf_machine.nHead_machine + 1)
									% buf_machine.BUF_SIZE;

						}
						//Log.i("buf_machine.nHead_machine=" + buf_machine.nHead_machine);
						//Log.i("buf_machine.nTail_machine=" + buf_machine.nTail_machine);
						buf_machine.bflag_synchronized = false;
						return true;
					}else{
						Cbuffer_machine.buffer_init();
						logger.info("buffer_get，handel_cmd_net,data exception,,,,,,,,,,,,");
						buf_machine.bflag_synchronized = false;
						return false;
					}
					
				}
			}
			
			
		}
		catch(Exception e){
			Cbuffer_machine.buffer_init();
			logger.info("handel_cmd_net,,,,,,,,,,,,,exception error");
		}
		return false;
	}
		
}
